package com.sh.apiconsume.service;

import com.google.gson.Gson;
import com.sh.apiconsume.model.QQToJMSMessage;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class JmsSendQQEmailConsumer {

    @Value("${spring.mail.username}")
    private String from;

    @Resource
    private JavaMailSender javaMailSender;

    @JmsListener(destination = "sendQQNum")
    @Async
    public void sendQQEmail(String message) {
        log.info("接收到消息：{}", message);
        log.info("我是MailBiz的Send，我的线程是：{}" , Thread.currentThread().getName());
        Gson gson = new Gson();
        QQToJMSMessage qqToJMSMessage = gson.fromJson(message, QQToJMSMessage.class);
        SimpleMailMessage messageTO = new SimpleMailMessage();
        messageTO.setFrom(from);
        messageTO.setTo(qqToJMSMessage.getQqEmail());
        messageTO.setSubject("邮箱验证");
        messageTO.setText("您的验证码是：" + qqToJMSMessage.getCaptcha());
        javaMailSender.send(messageTO);
        log.info("邮件发送成功", messageTO);
    }


}
